<?php

class Usuario_Model extends Modelo {

    function __construct() {
        parent::__construct();
    }
    
    public function listarUsuarios() {
                
       $consulta = $this->db->ejecutarConsulta('SELECT id, nombre, apellido, login, role FROM tbl_usuarios') ;
       
       return $consulta->fetchAll();
    }
    
    public function listarUsuarioIndividual($id) {
       $sql = 'SELECT id, nombre, apellido, login, role FROM tbl_usuarios WHERE id= :id';
       $consulta = $this->db->ejecutarConsulta($sql,array(':id' => $id)) ;
       return $consulta->fetch();
    }
    
    public function crear($data){
        $datos=array(
            'nombre'   => $data['nombre'],
            'apellido' => $data['apellido'],
            'login'    => $data['login'],
            'password' => Hash::create('md5',$data['password']),
            'role'     => $data['role']
        );
        try{
          $this->db->insertar('tbl_usuarios', $datos);  
        }  catch (Exception $e){
            exit($e->getMessage());
        }
        
        
    }
    
    public function guardarEditar($data){
       
        $datos=array(
            'nombre'   => $data['nombre'],
            'apellido' => $data['apellido'],
            'login'    => $data['login'],
            'password' => Hash::create('md5',$data['password']),
            'role'     => $data['role']
        );
        
         $this->db->actualizar('tbl_usuarios', $datos,"id={$data['id']}");
    }
    
    public function eliminar($id) {
        $sql = 'DELETE FROM tbl_usuarios WHERE id= :id';
        $consulta = $this->db->ejecutarConsulta($sql,array(':id' => $id));
    }
    
    public function resetPassword($data){
       
        $datos=array(
            'password' => Hash::create('md5','123456')
        );
        
         $this->db->actualizar('tbl_usuarios', $datos,"id={$data['id']}");
    }


}